<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="昵称" prop="nickName">
        <el-input
            v-model="queryParams.nickName"
            placeholder="请输入昵称"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input
            v-model="queryParams.phone"
            placeholder="请输入手机号"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['service:member:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="memberList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="用户编号" align="center" prop="id"/>
      <el-table-column label="头像" align="center">
        <template slot-scope="scope">
          <el-image style="width: 100px; height: 100px"
                    :src="headPortrait(scope.row.headPortrait)"></el-image>
        </template>
      </el-table-column>
      <el-table-column label="昵称" align="center" prop="nickName"/>
      <el-table-column label="手机号" align="center" prop="phone"/>
      <el-table-column label="性别" align="center" prop="sex">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.sex"/>
        </template>
      </el-table-column>
      <el-table-column label="出生日期" align="center" prop="birthday" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.birthday, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建日期" align="center" prop="createTime" width="180"/>
      <el-table-column label="账户状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-popover trigger="hover" placement="top" v-if="scope.row.status === -1">
            <p>不通过原因: {{ scope.row.verifyMessage }}</p>
            <div slot="reference" class="name-wrapper">
              <dict-tag :options="dict.type.member_status" :value="scope.row.status"/>
            </div>
          </el-popover>
          <dict-tag v-else :options="dict.type.member_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
              v-if="scope.row.status == 0"
              size="mini"
              type="text"
              icon="el-icon-close"
              @click="handleUpdate(scope.row,-1)"
              v-hasPermi="['service:member:edit']"
          >禁用
          </el-button>
          <el-button
              v-if="scope.row.status == -1"
              size="mini"
              type="text"
              icon="el-icon-check"
              @click="handleUpdate(scope.row,0)"
              v-hasPermi="['service:member:edit']"
          >启用
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
    />
  </div>
</template>

<script>
  import {listMember, getMember, delMember, addMember, updateMember} from "@/api/service/member";

  export default {
    name: "Member",
    dicts: ['sys_user_sex', 'member_status'],
    data() {
      return {
        baseImageUrl: process.env.VUE_APP_BASE_API,
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 会员表格数据
        memberList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          openId: null,
          headPortrait: null,
          nickName: null,
          phone: null,
          sex: null,
          birthday: null,
          status: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {}
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 头像地址 */
      headPortrait(headPortrait) {
        if (headPortrait) {
          return headPortrait.indexOf('http') !== -1 ? headPortrait : this.baseImageUrl + headPortrait;
        }
      },
      /** 查询会员列表 */
      getList() {
        this.loading = true;
        listMember(this.queryParams).then(response => {
          this.memberList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          openId: null,
          headPortrait: null,
          nickName: null,
          phone: null,
          sex: null,
          birthday: null,
          status: 0,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加会员";
      },
      /** 启用/禁用按钮操作 */
      handleUpdate(row, status) {
        this.reset();
        const id = row.id || this.ids

        if (status === 0) {
          this.$modal.confirm('是否确认启用选中的会员？').then(function () {
            return updateMember({id, status});
          }).then(() => {
            this.getList();
            this.$modal.msgSuccess("启用成功");
          }).catch(() => {
          });
        } else {
          this.$modal.prompt('请输入禁用的原因', '请输入禁用的原因').then((res) => {
            return updateMember({id: id, status: status, verifyMessage: res.value});
          }).then(() => {
            this.getList();
            this.$modal.msgSuccess("禁用成功");
          }).catch(() => {
          });
        }
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除会员编号为"' + ids + '"的数据项？').then(function () {
          return delMember(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {
        });
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('service/member/export', {
          ...this.queryParams
        }, `member_${new Date().getTime()}.xlsx`)
      }
    }
  };
</script>
